import { toFixed } from "@/filters";
import { last } from "cl-admin/utils";
import { mapGetters, mapState } from "vuex";
function firstToUpper(str) {
	return str.toLowerCase().replace(/( |^)[a-z]/g, (L) => L.toUpperCase());
}
export default {
	data() {
		return {
			initStatus: false,
			keyWordType: [
				{ label: "手机号", value: 1 },
				{ label: "微信号", value: 2 },
				{ label: "名称", value: 3 },
				{ label: "会员号", value: 4 }
			]
		}
	},
	computed: {
		...mapGetters(["token", "userInfo"]),
		...mapState("common", ["bankTypes"]),
		...mapState("biz", ["bizMap", "business", "bizList"])
	},
	activated() {
		if (!this.initStatus) return
		this.initData && this.initData();
	},
	deactivated() {
		this.initStatus = true
	},
	methods: {
		toFixed,
		getCurrBiz(hq = false) {
			const { biz_id } = this.userInfo;
			if (hq) {
				return biz_id;
			}
			if (biz_id != 1) {
				return biz_id;
			}
			return last(this.business)?.biz_id ?? null;
		},
		confirm(msg, options = {}) {
			return this.$confirm(msg || "是否执行此操作?", "提示", {
				type: "warning",
				...options
			});
		},
		// 获取事业部关联数据
		getBizRefData(biz_id, key) {
			let data = this.bizMap['biz' + biz_id] || {
				biz_id: null,
				company: [],
				brand: [],
				city: [],
				shop: [],
				shop_childs: [],
				region: [],
				level: []
			}
			if (key) {
				return data[key]
			}
			return data
		},

		// 更新事业部关联数据
		async updateBizRefData(biz_id, ignore = ['region', 'level'], refresh = true) {
			// this.getBizRefCompany(biz_id),
			// this.getBizRefBrand(biz_id),
			// this.getBizRefCity(biz_id),
			// this.getBizRefShop(biz_id),
			// this.getBizRefRegion(biz_id)
			let list = [
				'company',
				'brand',
				'city',
				'shop',
				'region',
				'level'
			]
			if (ignore && ignore.length) {
				list = list.filter(v => !ignore.includes(v))
			}
			let promises = list.map(v => this['getBizRef' + firstToUpper(v)](biz_id, refresh))
			return Promise.all(promises)
		},
		// 批量更新品牌数据
		async updateBraRefBiz(bra_id = 0, refresh = true) {
			let id = Number(bra_id)
			if (isNaN(id)) return
			let promises = this.bizList.filter(v => {
				if (!v.main_brands) return false
				return v.mange_brands.includes(bra_id + '')
			}).map(v => this.getBizRefBrand(v.biz_id, refresh))
			return Promise.all(promises)
		},
		// 获取事业部列表
		getBizList(has_headquarters) {
			return this.$store.dispatch("biz/getBusiness", has_headquarters);
		},
		// 获取事业部关联分公司
		async getBizRefCompany(biz_id, refresh = true) {
			let id = Number(biz_id)
			if (isNaN(id)) return []
			let res = this.getBizRefData(biz_id, 'company')
			if (res.length && !refresh) return res
			return this.$store.dispatch("biz/getCompany", biz_id);
		},
		// 获取事业部关联品牌
		async getBizRefBrand(biz_id, refresh = true) {
			let id = Number(biz_id)
			if (isNaN(id)) return []
			let res = this.getBizRefData(biz_id, 'brand')
			if (res.length && !refresh) return res
			return this.$store.dispatch("biz/getBrand", biz_id);
		},
		// 获取事业部关联城市
		async getBizRefCity(biz_id, refresh = true) {
			let id = Number(biz_id)
			if (isNaN(id)) return []
			let res = this.getBizRefData(biz_id, 'city')
			if (res.length && !refresh) return res
			return this.$store.dispatch("biz/getCity", biz_id);
		},
		// 获取事业部关联门店
		async getBizRefShop(biz_id, refresh = true) {
			let id = Number(biz_id)
			if (isNaN(id)) return []
			let res = this.getBizRefData(biz_id, 'shop')
			if (res.length && !refresh) return res
			return this.$store.dispatch("biz/getShop", biz_id);
		},
		// 获取事业部关联区域
		async getBizRefRegion(biz_id, refresh = true) {
			let id = Number(biz_id)
			if (isNaN(id)) return []
			let res = this.getBizRefData(biz_id, 'region')
			if (res.length && !refresh) return res
			return this.$store.dispatch("biz/getRegion", biz_id);
		},
		// 获取事业部关联等级
		async getBizRefLevel(biz_id, refresh = true) {
			let id = Number(biz_id)
			if (isNaN(id)) return []
			let res = this.getBizRefData(biz_id, 'level')
			if (res.length && !refresh) return res
			return this.$store.dispatch("biz/getLevel", biz_id);
		}

	}
};
